package com.yvnze.ydyp.modules.document.mapper;

import com.yvnze.ydyp.modules.document.entity.File;
import com.yvnze.ydyp.modules.document.entity.FileRelation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Haipeng Wang  
 * @since 2021-08-14
 */
@Mapper
public interface FileRelationMapper extends BaseMapper<FileRelation> {

    @Insert("INSERT INTO file_relation(ancestor,descendant,distance) (SELECT ancestor,#{ancestor},distance+1 FROM file_relation WHERE descendant=#{descendant})")
    public void insertNode(FileRelation fileRelation);

    /**
     * 查找fileInfoId 对应的节点的所有直属子节点
     * 就是看有哪些节点的祖先指针是fileInfoId
     * 这些节点和fileInfoId 对应节点的距离是1
     * @param fileInfoId
     * @return
     */
    @Select("SELECT descendant FROM file_relation WHERE ancestor = #{fileInfoId} AND distance = 1 AND deleted = 0")
    public List<FileRelation> selectDirNode(String fileInfoId);


    /**
     * 查找fileInfoId 对应的节点的所有直属子节点
     * 就是看有哪些节点的祖先指针是fileInfoId
     * 这些节点和fileInfoId 对应节点的距离是1
     * @param fileInfoId
     * @return
     */
    @Select("SELECT descendant FROM file_relation WHERE ancestor = #{fileInfoId} AND distance > 0 AND deleted = 0")
    public List<FileRelation> selectAllDirNode(String fileInfoId);


    /**
     * 根据FileInfoId 删除一个没有子树的节点
     * @param fileInfoId
     */
    @Update("UPDATE FROM file_relation SET deleted = 1 WHERE descendant = #{fileInfoId}")
    void delete(String fileInfoId);


    /**
     * 根据FileInfoId 级联删除所有相关的记录
     * @param fileInfoId
     */
    public void deleteCascade(String fileInfoId);



}
